# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：get_stock_anotice.py
#日期：2021-08-29
#备注：获取东方财富网的股票公告信息   http://data.eastmoney.com/notices/hsa/2.html
http://data.eastmoney.com/notices/getdata.ashx?StockCode=300470&FirstNodeType=2&CodeType=A&PageIndex=1&PageSize=50
http://data.eastmoney.com/notices/getdata.ashx?FirstNodeType=2&CodeType=A&PageIndex=1&PageSize=50
drop table if exists stock_notices;
CREATE TABLE `stock_notices` (
  `stock_no` varchar(32) NOT NULL COMMENT '股票代码',
  `stock_name` varchar(32) NOT NULL COMMENT '股票简称',
  `notice_date` varchar(32) NOT NULL COMMENT '公告日期',
  `notice_code` varchar(32) NOT NULL COMMENT '公告代码',
  `notice_name` varchar(32) NOT NULL COMMENT '公告类型',
  `notice_title` varchar(200) NOT NULL COMMENT '公告名称',   
  `process` int(3) NOT NULL COMMENT '流程：1-增发预案，2-审核通过，3-核准批复，4-发行成功,591-激励计划(草案),593-激励(授予日)',
  `price_type` int(3) NOT NULL DEFAULT '0' COMMENT '定价规则：0-竞价，1-定价,3-回购激励，4-增发激励',
  `base_date` varchar(32) NOT NULL COMMENT '定增基准日/授予登记日',
  `price` float NOT NULL DEFAULT '0' COMMENT '定增价格/激励价格',   
  `price_md` float NOT NULL DEFAULT '0' COMMENT '股本改变后的定增价格/激励价格',   
  `create_time` datetime NOT NULL COMMENT '创建时间',
  PRIMARY KEY (`stock_no`,`process`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='沪深股票定增公告信息';
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import requests
from pacong.stock.stock_config import db,cursor,headers
from pycacho.cachobase.logger import Logger

logger = Logger("get_stock_add").get_log()


notice ={
    "重大事项":{
        "f_node":"5",
        "重大合同": "7",
        "投资相关": "8",
        "股权激励":{
            "s_node":"9",
            "001002007001001":"股权激励计划"
        },
        "全部":"0"
    },
}

# 获取
def stock_add_notice(stock_code,n,f_node,s_node):
    url = 'http://np-anotice-stock.eastmoney.com/api/security/ann?page_size=50&ann_type=A' \
          '&stock_list={0}&page_index={1}&f_node={2}&s_node={3}'.format(stock_code,n,f_node,s_node)
    print(url)
    resp = requests.get(url, headers=headers).json()
    data = resp['data']['list']
    #print(data)
    for info in data:
        print(info)
        stock_no = info['codes'][0]['stock_code']
        stock_name = info['codes'][0]['short_name']
        notice_code = info['columns'][0]['column_code']
        notice_name = info['columns'][0]['column_name']
        notice_title = info['title']
        notice_date = info['notice_date'][0:10]
        if notice_code == '001002007001001' and '激励计划(草案)' in notice_title and stock_no[0:3]!='688':
            process = '591'
            print(stock_no,stock_name,notice_date,notice_code,notice_name,notice_title,process)
            #notice_to_mysql(stock_no, stock_name, notice_date, notice_code, notice_name, notice_title, process)
        elif notice_code == '001002007001003' and '(授予日)' in notice_title and stock_no[0:3]!='688':
            process = '593'
            print(stock_no, stock_name, notice_date, notice_code, notice_name, notice_title, process)
            #notice_to_mysql(stock_no, stock_name, notice_date, notice_code, notice_name, notice_title, process)
# 沪深重要指数
def notice_to_mysql(stock_no, stock_name, notice_date, notice_code,notice_name, notice_title, process):
    sql = "insert into stock_notices (stock_no, stock_name, notice_date, notice_code,notice_name, notice_title, process,price_type,price,price_md,create_time,remark) " \
          "values ('%s','%s','%s','%s','%s','%s','%s','回购激励',0,0,now(),'');" \
          % (stock_no, stock_name, notice_date, notice_code,notice_name, notice_title, process)
    try:
        cursor.execute(sql)
    except Exception as e:
        # 发生错误时回滚
        db.rollback()
        print(str(e))
    else:
        db.commit()
        #logger.info('事务处理成功')


if __name__ == '__main__':
    type = notice['重大事项']
    for i in range(1,2):
        stock_add_notice('002023',i,type['f_node'],type['股权激励']['s_node'])
